package com.TaskOrganizer.dialogs;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.TaskOrganizer.gui.MainPanel;

public class MainDialog extends JFrame {

	private static final long serialVersionUID = 7244585453598064018L;
	private JPanel mainPanel = null;
	private TrayIcon trayIcon;
	private SystemTray tray;

	public MainDialog(String version) {
		URL url = getClass().getResource("/com/TaskOrganizer/icons/icon.png");
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image img = kit.createImage(url);
		setIconImage(img);
		if (SystemTray.isSupported()) {
			tray = SystemTray.getSystemTray();
			ActionListener exitListener = new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			};

			PopupMenu popup = new PopupMenu();
			MenuItem defaultItem = new MenuItem("Maximize");
			defaultItem.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					setVisible(true);
					setExtendedState(JFrame.NORMAL);
				}
			});
			popup.add(defaultItem);
			defaultItem = new MenuItem("Exit");
			defaultItem.addActionListener(exitListener);
			popup.add(defaultItem);

			url = getClass().getResource("/com/TaskOrganizer/icons/icon.png");
			img = kit.createImage(url);
			trayIcon = new TrayIcon(img, "TaskOrganizer", popup);
			trayIcon.setImageAutoSize(true);
			trayIcon.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					tray.remove(trayIcon);
					activate();
				}
			});
		}
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setResizable(false);
		setTitle("TaskOrganizer V" + version);
		add(getMainPanel(), BorderLayout.SOUTH);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(this);
		pack();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);

		addWindowStateListener(new WindowStateListener() {

			public void windowStateChanged(WindowEvent e) {
				if (e.getNewState() == ICONIFIED) {
					try {
						tray.add(trayIcon);
						setVisible(false);
					} catch (AWTException ex) {
						System.out.println("unable to add to tray");
					}
				}
				if (e.getNewState() == 7) {
					try {
						tray.add(trayIcon);
						setVisible(false);
					} catch (AWTException ex) {
						System.out.println("unable to add to system tray");
					}
				}
				if (e.getNewState() == MAXIMIZED_BOTH) {
					tray.remove(trayIcon);
					setVisible(true);
				}
				if (e.getNewState() == NORMAL) {
					tray.remove(trayIcon);
					setVisible(true);
				}
			}
		});

	}

	private JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new MainPanel();
		}
		return mainPanel;
	}

	private void activate() {
		setVisible(true);
		int state = getExtendedState();
		state &= ~JFrame.ICONIFIED;
		setExtendedState(state);
		toFront();
		requestFocus();
	}

}
